var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

function emailTo(email, subject, text, html, callback) {
  // 开启一个 SMTP 连接池
  var transporter = nodemailer.createTransport(smtpTransport({
    host: 'smtp.qq.com',//主机
    secure: true, // 使用 SSL
    secureConnection: true, // 使用 SSL
    port: 465, // SMTP 端口
    auth: {
      user: '3028817568@qq.com',
      pass: 'bjiardhudnybdcha' //授权码,通过QQ获取  

    }
  }));
  var mailOptions = {
    from: '3028817568@qq.com', // 发送者  
    to: email, // 接受者,可以同时发送多个,以逗号隔开  
    subject: subject, // 标题  
  }; 24
  if (text) {
    mailOptions.text = text;// 文本  30
  } else {
    mailOptions.html = html;
  }
  var result = {
    httpCode: 200,
    data: [],
  }
  try {
    transporter.sendMail(mailOptions, function (err, info) {
      if (err) {
        result.httpCode = 500;
        result.message = err;
        callback(result); return;
      }
      callback(result);
    });
  } catch (err) {
    result.httpCode = 500;
    result.message = err;
    callback(result);
  }
  transporter.close(); // 如果没用，关闭连接池

}

module.exports = {
  emailTo
}